import { addCareLevel, deleteCareLevel, queryLevelList, queryTagList, queryWayList } from '@/api';
import { generateOptionsAndMap } from '@/tools/enum';
import type { SelectMap, SelectOption } from '@/types/option';
import { defineStore } from 'pinia';
import { ref } from 'vue';


export interface CareState {
	levels: SelectOption[];
	deleteLevel: (id: number) => Promise<void>;
	queryLevels: () => Promise<void>;
	addLevel: (name: string) => Promise<void>;
	levelsMap: SelectMap
}

export const useCareStore = defineStore('care-info', () => {
	const levels = ref<SelectOption[]>([]);
	const ways = ref<SelectOption[]>([]);
	const tags = ref<SelectOption[]>([]);
	const levelsMap = ref<SelectMap>(new Map())

	function queryTags() {
		return queryTagList().then(res => {
			tags.value = generateOptionsAndMap(res.Data).options;
		})
	}
	function queryLevels() {
		return queryLevelList().then(res => {
			const { options, map } = generateOptionsAndMap(res.Data?.Levels);
			levels.value = options;
			levelsMap.value = map;
		})
	}

	function deleteLevel(id: number) {
		return deleteCareLevel(id).then(() => {
			queryLevels();
		})
	}

	function addLevel(name: string) {
		return addCareLevel(name).then(() => {
			queryLevels();
		});
	}

	function queryWay() {
		queryWayList().then((res) => {
			ways.value = generateOptionsAndMap(res.Data?.Ways).options;
		})
	}

	return {
		levels,
		queryLevels,
		deleteLevel,
		addLevel,
		queryWay,
		ways,
		queryTags,
		tags,
		levelsMap
	};
});
